<template>
  <div>
    <div class="container" v-if="dialogVisible">
      <el-dialog title="提示" :visible="dialogVisible" width="900px" @close="close">
        <el-row class="text">
          <el-col :span="6">
            <span v-if="questionItem.questionType === '1'">【题型】：单选题</span>
            <span v-else-if="questionItem.questionType === '2'">【题型】：多选题</span>
            <span v-else>【题型】：简答题</span>
          </el-col>
          <el-col :span="6">
            <span> 【编号】：{{ questionItem.id }}</span>
          </el-col>
          <el-col :span="6">
            <span v-if="questionItem.difficulty === '1'">【题型】：简单</span>
            <span v-else-if="questionItem.difficulty === '2'">【题型】：一般</span>
            <span v-else>【题型】：困难</span>
          </el-col>
          <el-col :span="6">
            <span> 【标签】：{{ questionItem.tags }}</span>
          </el-col>
        </el-row>
        <el-row class="text">
          <el-col :span="6">
            <span>【学科】：{{ questionItem.subject }}</span>
          </el-col>
          <el-col :span="6">
            <span>【目录】：{{ questionItem.directoryName }}</span>
          </el-col>
          <el-col :span="6">
            <span>【方向】：{{ questionItem.direction }}</span>
          </el-col>
          <el-col :span="6"> </el-col>
        </el-row>
        <hr />
        <!-- 题干部分 -->
        <el-row>
          <el-col>
            <span>【题干】：</span>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="color:blue;">
            <div v-html="questionItem.question"></div>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <span>
              <span v-if="questionItem.questionType === '1'">单选题</span>
              <span v-else-if="questionItem.questionType === '2'">多选题</span>
              <span v-else>简答题</span>选项：（以下选中的选项为正确答案）
            </span>
          </el-col>
        </el-row>
        <!-- 单选框 -->
        <!-- data中的数据 radio: 1  由于单选框只有一个isRight 判断相等 按钮选中-->
        <el-radio-group v-model="radio" v-if="questionItem.questionType === '1'" @change="change">
          <el-row v-for="(item, index) in questionItem.options" :key="index" style="margin:10px 0px">
            <el-radio :label="item.isRight">{{ item.title }}</el-radio>
          </el-row>
        </el-radio-group>
        <!-- 多选框 -->
        <div v-if="questionItem.questionType === '2'">
          <el-row v-for="(item, index) in options" :key="index" style="margin:10px 0px">
            <el-checkbox v-model="item.isRight" :disabled="item.isRight === 0" @change="testChange(item)">
              {{ item.title }}
            </el-checkbox>
          </el-row>
        </div>
        <hr />
        【参考答案】：<el-button type="danger" @click="isVideoShow = true">视频答案预览</el-button>
        <el-row style="margin-top:20px;" v-if="isVideoShow">
          <video :src="questionItem.videoURL" controls="controls" class="video"></video>
        </el-row>
        <hr />
        <el-row class="answer">
          <span>【答案解析】：https://cn.vuejs.org/ 有答案 </span>
        </el-row>

        <hr />
        <el-row class="answer">
          <span>
            【题目备注】：https://cn.vuejs.org/ 有答案
          </span>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="close">关 闭</el-button>
        </span>
      </el-dialog>
    </div>

    <div class="container" v-if="dialogShow">
      <el-dialog @close="cancel" title="题目预览" :visible="dialogShow" width="50%">
        <div class="qusetionInfo">
          <el-row>
            <el-col :span="6"
              ><span>【题型】：{{ detailList.questionType | handelQuestionType }}题</span></el-col
            >
            <el-col :span="6"
              ><span>【编号】：{{ detailList.id }}</span></el-col
            >
            <el-col :span="6"
              ><span>【难度】：{{ detailList.difficulty | handelDifficulty }}</span></el-col
            >
            <el-col :span="6"
              ><span>【标签】：{{ detailList.tags }}</span></el-col
            >
          </el-row>
          <el-row class="row2">
            <el-col :span="6"
              ><span>【学科】：{{ rowList.subject }}</span></el-col
            >
            <el-col :span="6"
              ><span>【目录】：{{ rowList.catalog }}</span></el-col
            >
            <el-col :span="6"
              ><span>【方向】：{{ detailList.direction }}</span></el-col
            >
          </el-row>
        </div>
        <div>
          <p>【题干】：</p>
          <p class="stem" v-html="detailList.question"></p>
          <div v-if="detailList.questionType === '3'"></div>
          <div v-else>
            <p>{{ detailList.questionType | handelQuestionType }}题 选项：（以下选中的选项为正确答案）</p>
            <el-radio-group v-if="detailList.questionType === '1'" v-model="radio" class="radio_box">
              <el-radio v-for="item in detailList.options" :key="item.id" :label="item.isRight">{{
                item.title
              }}</el-radio>
            </el-radio-group>
            <el-checkbox-group class="checkBOx" v-model="checkList" v-else-if="detailList.questionType === '2'">
              <el-checkbox v-for="item in detailList.options" :key="item.id" :label="item.title"></el-checkbox>
            </el-checkbox-group>
          </div>
          <hr />
          <div>
            <span>【参考答案】：</span>
            <span><el-button type="danger" @click="getVideoURL">视频答案预览</el-button></span>
            <div class="videoBox" ref="video">
              <video v-if="videoURL" :src="videoURL" controls></video>
            </div>
          </div>
          <hr />
          <div class="answer">
            <span>【答案解析】：</span>
            <span v-html="detailList.answer"></span>
          </div>
          <hr />
          <div class="remarks">
            <span>【题目备注】:</span>
            <span v-html="detailList.remarks"></span>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { detail } from '@/api/hmmm/questions.js'
export default {
  data() {
    return {
      isVideoShow: false,

      // 后边加的数据 ********************************************
      rowList: [],
      detailList: [],
      radio: 1,
      videoURL: '',
      checkList: []
    }
  },
  props: ['dialogVisible', 'questionItem', 'dialogShow', 'rowData'],
  // props: {
  //   dialogVisible: { type: Boolean },
  //   questionItem: { type: Object },

  //   // 新加的*************************************************
  //   dialogShow: { type: Boolean, default: false },
  //   rowData: { type: Object }
  // },
  methods: {
    //  多选框的change事件
    testChange(item) {
      if (item.isRight) {
        console.log(item.isRight)
      } else {
        item.isRight = true
      }
    },

    // 单选框点击事件
    change() {
      this.radio = 1
    },
    // 关闭弹框的事件
    close() {
      this.$emit('update:dialogVisible', false)
      this.isVideoShow = false
    },

    // 新加的***************************************************
    cancel() {
      this.$emit('update:dialogShow', false)
    },
    getVideoURL() {
      this.videoURL = this.rowList.videoURL
      if (this.videoURL) {
        this.$refs.video.style.display = 'block'
      }
    },
    getCheckList(arg) {
      if (arg) {
        const options = arg.options
        options.forEach(item => {
          if (item.isRight === 1) {
            this.checkList.push(item.title)
          }
        })
      }
      console.log(this.checkList)
    }
  },
  computed: {
    options() {
      // 先判断是不是多选题
      if (this.questionItem.questionType === '2') {
        if (!this.questionItem.options) {
          return // 如果没有题目选项停止后续操作
        }
        // 遍历传递过来的题目选项 如果isRight===1 代表该选项是被选中的选项
        this.questionItem.options.forEach(element => {
          if (element.isRight === 1) {
            // 将这一项的isRight改为true 多选框就会默认选择
            element.isRight = true
          }
        })
        // 修改完成之后
        return this.questionItem.options
      } else {
        // 如果不是单选题 原路返回传递过来的题目项
        return this.questionItem.options
      }
    }
  },
  watch: {
    async rowData(newVal, oldVal) {
      this.rowList = newVal
      console.log(this.rowList)
      if (newVal) {
        const res = await detail(this.rowList)
        this.detailList = res.data
        this.getCheckList(this.detailList)
        console.log(this.detailList)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  .video {
    width: 400px;
  }
  .answer {
    padding: 8px 0px;
  }
  .el-row {
    .el-col {
      span {
        font-size: 14px;
        color: #606266;
        line-height: 36px;
      }
    }
  }
  .qusetionInfo {
    padding-bottom: 15px;
    border-bottom: 1px solid #a2a2a2;
    .row2 {
      margin-top: 10px;
    }
  }
  .stem {
    color: #5858ff;
  }
  .radio_box,
  .checkBOx {
    display: flex;
    flex-direction: column;
  }
  ::v-deep label.el-radio {
    height: 33px;
    line-height: 33px;
  }
  ::v-deep .el-radio-group.radio_box {
    margin-top: -10px;
  }
  ::v-deep label.el-checkbox {
    margin-bottom: 12px;
  }
  .videoBox {
    width: 400px;
    height: 300px;
    display: none;
    video {
      width: 100%;
      height: 100%;
    }
  }
  .answer,
  .remarks {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
  }
}
</style>
